<!--
 * @Author: 噜噜雯雯 2199987619@qq.com
 * @Date: 2025-05-30 15:00:30
 * @LastEditors: 噜噜雯雯 2199987619@qq.com
 * @LastEditTime: 2025-09-28 15:42:40
 * @FilePath: \linePatrol\vue-element-plus-admin\src\views\Authorization\Dictionary\components\EditDictType.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<script setup lang="ts">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch, ref, unref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { ElMessage } from 'element-plus'
import { Dialog } from '@/components/Dialog'
import TransferData from './TransferData.vue'
import { FacTaskListType } from '@/api/linePatrol/type'
const { required } = useValidator()
const taskCount = ref<any>(0)
const props = defineProps({
  currentRow: {
    type: Object as PropType<FacTaskListType>,
    default: () => null
  },
  formSchema: {
    type: Array as PropType<FormSchema[]>,
    default: () => []
  },
  actionType: {
    type: String as PropType<string>,
    default: () => null
  }
})

if (props.actionType == 'edit') {
  taskCount.value = props.currentRow.taskCount
}

//必填项
const rules = reactive({
  taskType: [required()],
  taskName: [required()],
  taskState: [required()]
})
const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods
const FormHeaduuid = ref([])
const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    formData['taskFaclist'] = FormHeaduuid.value
    return formData
  }
}

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

const dialogVisible = ref(false)
const saveLoading = ref(false)
const TransferTypeRef = ref()
const save = async () => {
  const write = unref(TransferTypeRef)
  const formData = await write?.submit()
  FormHeaduuid.value = formData.value.map((item) => {
    return item.uuid
  })
  taskCount.value = FormHeaduuid.value.length + taskCount.value
  saveLoading.value = false
  dialogVisible.value = false
}
const taskId = ref<any>(0)
const ChooseTask = async () => {
  const form = await getFormData()
  if (props.actionType == 'edit') {
    taskId.value = props.currentRow.id
  }
  if (form.taskType) {
    dialogVisible.value = true
  } else {
    ElMessage({
      message: '请先选择任务类型',
      type: 'warning'
    })
  }
}
defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="formSchema" />
  <span class="FormHead">&emsp;&emsp;任务清单&emsp;</span
  ><BaseButton size="small" @click="ChooseTask" type="primary">添加任务</BaseButton>
  <div class="el-upload__tip ml-[10px]" style="display: inline-block">
    已添加{{ taskCount }}个任务
  </div>
  <Dialog style="width: 90% !important" v-model="dialogVisible" title="任务选择">
    <TransferData ref="TransferTypeRef" :taskId="taskId" />
    <template #footer>
      <BaseButton type="primary" :loading="saveLoading" @click="save"> 提交 </BaseButton>
      <BaseButton @click="dialogVisible = false">关闭 </BaseButton>
    </template>
  </Dialog>
</template>
<style scoped>
/* ::v-deep是vue3提供的深度选择器，.el-form-item__label是element-plus官方定义的类 */
.FormHead {
  /* font-size: 18px; */
  font-weight: 600;
}
:deep(.el-col) {
  margin: 5px 0;
}
:deep(.el-form-item__label) {
  display: inline-block !important;
}
</style>
